package com.dllo.lanou.cookapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dllo.lanou.cookapp.R;
import com.dllo.lanou.cookapp.entity.BookListEntity;
import com.dllo.lanou.cookapp.volly.GetVolleyImage;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by xijinhua on 2016/3/19.
 */
public class BookListAdapter extends BaseAdapter {
    private List<BookListEntity> bookListEntities;
    private Context context;
    private LayoutInflater mInflater;
    public BookListAdapter(Context context){
        mInflater = LayoutInflater.from(context);
    }
    public void addDatas(List<BookListEntity> bookListEntities){
        this.bookListEntities = bookListEntities;
        notifyDataSetChanged();

    }

    @Override
    public int getCount() {
        return bookListEntities != null && bookListEntities.size()>0
                ?bookListEntities.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return bookListEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView =mInflater.inflate(R.layout.item_bookgridview,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();

        }

        BookListEntity bookListEntity = bookListEntities.get(position);
        holder.bookname.setText(bookListEntity.getName());
        if (bookListEntity.getImg() != null){
            String url = "http://tnfs.tngou.net/image"+ bookListEntity.getImg();
            GetVolleyImage.getNetWorkImg(url,holder.book_img);
        }

        return convertView;
    }

    static class ViewHolder{
        @Bind(R.id.book_name)
        TextView bookname;
        @Bind(R.id.book_img)
        ImageView book_img;
        public  ViewHolder (View view){
            ButterKnife.bind(this, view);
        }
    }
}
